package com.wst.wst_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.wst_server.entity.Money;
import com.wst.wst_server.entity.Org;
import com.wst.wst_server.entity.Orgplus;
import com.wst.wst_server.mapper.OrgMapper;
import com.wst.wst_server.mapper.OrgplusMapper;
import com.wst.wst_server.service.IOrgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 * ��֯������ 服务实现类
 * </p>
 *
 * @author zyk
 * @since 2023-03-24
 */
@Service
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements IOrgService {

    @Autowired
    OrgMapper orgMapper;

    @Autowired
    OrgplusMapper orgplusMapper;


    @Override
    public boolean add(Org org, Orgplus orgplus) {
        orgMapper.insert(org);
        orgplus.setOrgId(org.getId());
        orgplusMapper.insert(orgplus);
        return true;
    }

    @Override
    public Map<String, Object> getId(Integer id) {
        Org org = orgMapper.selectById(id);
        Orgplus orgplus = orgplusMapper.selectOne(new QueryWrapper<Orgplus>().eq("orgId",id));
        Map<String ,Object> map = new HashMap<>();
        map.put("org",org);
        map.put("orgplus",orgplus);
        return map;
    }

    @Override
    public IPage<Org> getbycondition(Map<String, Object> map) {
        QueryWrapper<Org> queryWrapper = new QueryWrapper<Org>().orderByDesc("id");
        //遍历map集合
        Set<String> keys = map.keySet();
        for(String key : keys){
            if("page".equals(key) || "count".equals(key)){
                continue;
            }else {
                boolean condition = map.get(key) != null && StringUtils.hasLength(map.get(key).toString());
                if("orgName".equals(key) || "orgCode".equals(key)){
                    queryWrapper.like(key,map.get(key));
                }else {
                    queryWrapper.eq(key,map.get(key));
                }
            }
        }
        Integer page = Integer.parseInt(map.get("page").toString());
        Integer count = Integer.parseInt(map.get("count").toString());
        IPage<Org> p = new Page<>(page,count);
        return orgMapper.selectPage(p,queryWrapper);
    }

    @Override
    @Transactional
    public void myupdate(Org org, Orgplus orgplus) {
        orgMapper.updateById(org);
        orgplusMapper.update(orgplus,new QueryWrapper<Orgplus>().eq("orgId",org.getId()));
    }

    @Override
    public IPage<Map<String, Object>> getorgandorgplus(Map<String, Object> map) {
        Page page = new Page(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("count").toString()));
        QueryWrapper queryWrapper = new QueryWrapper();
        Set<String> keys = map.keySet();
        for (String key : keys) {
            if("page".equals(key) || "count".equals(key))
                continue;
            else 
                queryWrapper.eq(key,map.get(key));

        }
        return orgMapper.getorgandorgplus(page,queryWrapper);
    }

    @Override
    public Map<String, Object> getorgnameandyearly(Map<String, Object> map) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(map.containsKey("yearly"))
            queryWrapper.eq("yearly",map.get("yearly"));
        if(!(Integer.parseInt(map.get("orgId[0]").toString())==0 && map.size() == 2)){
            List<Integer> ids =new ArrayList<>();
            Set<String> keys = map.keySet();
            for (String key : keys){
                if(!key.equals("yearly")){
                    ids.add(Integer.parseInt(map.get(key).toString()));
                }
            }
            queryWrapper.in("orgid",ids);
        }
        return orgMapper.getorgnameandyearly(queryWrapper);
    }
}
